using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace ZerAx.TBON
{
    public class TbonByteArray : TbonObject
    {
        static TbonByteArray()
        {
            factories.Add(TbonType.ByteArray, (s) => new TbonByteArray(s));
        }

        public override TbonType Type => TbonType.ByteArray;

        private readonly List<byte> val = new();

        public TbonByteArray(string name) : base(name) { }
        public TbonByteArray(string name, params byte[] arr) : this(name)
        {
            val.AddRange(arr);
        }

        public override string GetValueString() => string.Join(' ', val.Select(x => x.ToString()));

        public override void ReadValue(BinaryReader reader)
        {
            int len = reader.ReadInt32();
            val.Clear();
            for (int i = 0; i < len; i++)
            {
                val.Add(reader.ReadByte());
            }
        }

        public override void WriteValue(BinaryWriter writer)
        {
            writer.Write(val.Count);
            foreach (byte b in val)
            {
                writer.Write(b);
            }
        }
    }
}